// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

mod app_config;
mod clipboard;
mod command;
mod screenshot_from_remote;
mod screenshot_local;
mod screenshot_and_save;
mod shortcut_manage;
mod tray_menu;
mod types;
mod save_to_file;
mod img_utils;
use std::{collections::HashMap, sync::Mutex};

use crate::app_config::{AppConfig, Storage};

#[tokio::main]
async fn main() {
    tauri::Builder::default()
        .plugin(tauri_plugin_dialog::init())
        .plugin(tauri_plugin_http::init())
        .plugin(tauri_plugin_notification::init())
        .plugin(tauri_plugin_shell::init())
        .plugin(
            tauri_plugin_global_shortcut::Builder::with_handler(shortcut_manage::shortcut_handler)
                .build(),
        )
        .manage(Storage {
            app_config: Mutex::new(AppConfig {
                local_hotkey: Some(String::from("Alt+A")),
                local_screen_target: Some(String::from("primary")),
                remote_hotkey: None,
                save_type_list: Some(vec![String::from("clipboard")]),
                save_path: None,
                remote_address: None,
                auto_create_save_path: None,
                img_save_type: Some(String::from("jpg")),
            }),
            shortcut_map: Mutex::new(HashMap::new()),
        })
        .setup(|app| {
            app_config::init(app)?;
            tray_menu::init(app)
        })
        .invoke_handler(tauri::generate_handler![
            command::get_config,
            command::set_config
        ])
        .on_window_event(|window, event| {
            if window.label() == "main" {
                match event {
                    tauri::WindowEvent::CloseRequested { api, .. } => {
                        let _ = window.hide();
                        api.prevent_close();
                    }
                    _ => {}
                }
            }
        })
        .enable_macos_default_menu(false)
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}
